import { defineStore } from "pinia";
import { reqC1, reqC2, reqC3 } from "@/api/product/attr";
import type { CategotyResponseData } from "@/api/product/attr/type";
import type { CategoryState } from "./types/type";
// 商品分类全局组件仓库
const useCategoryStore = defineStore('Category', {
    state: (): CategoryState => {
        return {
            // 存储一级分类的数据
            c1Arr: [],
            // 存储一级分类的id
            c1Id: '',
            // 存储二级分类的数据
            c2Arr: [],
            // 存储二级发呢类的id
            c2Id: '',
            // 存储三级分类的数据
            c3Arr: [],
            // 存储三级分类的id
            c3Id:''
        }
    },
    actions: {
        // 获取一级分类方法
        async getC1() {
            const result: CategotyResponseData = await reqC1()
            if (result.code == 200) {
                this.c1Arr = result.data
            }
        },
        // 获取二级分类方法
        async getC2() {
            const result: CategotyResponseData = await reqC2(this.c1Id)
            if (result.code == 200) {
                this.c2Arr = result.data
            }
        },
        // 获取三级分类方法
        async getC3() {
            const result: CategotyResponseData = await reqC3(this.c2Id)
            if (result.code == 200) {
                this.c3Arr = result.data
            }
        }
    },
    getters: {

    }
})

export default useCategoryStore